package com.Stash.Controller;


import com.Stash.Domain.*;
import com.Stash.Service.AdminService;
import com.Stash.Service.GeneralService;
import com.Stash.Utils.Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;


@RestController // 将web变成bean，并将最后的return的数据作为整体返回给前端
//@RequestMapping("/books") // 用于修饰类，则是该类所有方法的总路径，修饰方法则是方法的路径

@RequestMapping("/Admin")
public class AdminController {

    @Autowired //自动装配
    private GeneralService General;
    @Autowired //自动装配
    private AdminService admin;


    //    分页查询所有库存分类
    @PostMapping("/classificationLimit")
    public Result classificationLimit(@RequestBody Page page) {
        List<Classification> classifications = General.ClassificationSelectAll();
        if (page.getPageIndex()-1 < 0) {
            return new Result(Code.ERROR.getValue(), "给予的参数存在漏洞");
        }

        page.setPageIndex((page.getPageIndex()-1) * page.getPageSize());
        List<Classification> classificationsPage = admin.classificationLimit(page);

        HashMap<Object, Object> map = new HashMap<>();
        map.put("total", classifications.size());
        map.put("Data", classificationsPage);
        return new Result(Code.SUCCESS.getValue(), "查询成功", map);
    }

    //    添加分类信息
    @PostMapping("/classificationAdd")
    public Result classificationAdd(@RequestBody Classification classification) {
        List<Classification> classifications = General.ClassificationSelectAll();

        char c = classification.getClassificationName().charAt(classification.getClassificationName().length() - 1);
        char l = '类';
        if (c != l) {
            classification.setClassificationName(classification.getClassificationName() + "类");
        }
        List<Classification> collect = classifications.stream().filter(el -> {
            return el.getClassificationName().equals(classification.getClassificationName());
        }).collect(Collectors.toList());

        if (collect.size() > 0) {
            return new Result(Code.ERROR.getValue(), "分类名重复");
        }
        // 分类最多不超过十六个
        if (classifications.size() >= 16) {
            return new Result(Code.ERROR.getValue(), "分类数量到达上线");
        }
        classification.setClassificationTime(new Date());
        admin.classificationAdd(classification);
        return new Result(Code.SUCCESS.getValue(), "添加成功");
    }

    //   根据分类名称精准查询商品
    @PostMapping("/classificationSelectName")
    public Result classificationSelectName(@RequestBody Classification classification) {
        char c = classification.getClassificationName().charAt(classification.getClassificationName().length() - 1);
        char l = '类';
        if (c != l) {
            classification.setClassificationName(classification.getClassificationName() + "类");
        }
        Classification classification1 = admin.classificationSelectName(classification);
        if (classification1 == null) {
            return new Result(Code.ERROR.getValue(), "查询失败");
        }
        return new Result(Code.SUCCESS.getValue(), "查询成功", classification1);
    }

    //   根据分类id修改分类名称
    @PostMapping("/classificationUpdateName")
    public Result classificationUpdateName(@RequestBody Classification classification) {
        char c = classification.getClassificationName().charAt(classification.getClassificationName().length() - 1);
        char l = '类';
        if (c != l) {
            classification.setClassificationName(classification.getClassificationName() + "类");
        }
        List<Classification> classifications = General.ClassificationSelectAll();
        List<Classification> collect = classifications.stream().filter(el -> {
            return el.getClassificationName().equals(classification.getClassificationName());
        }).collect(Collectors.toList());

        if (collect.size() > 0) {
            return new Result(Code.ERROR.getValue(), "该分类名称存在，请勿重复添加");
        }
        admin.classificationUpdateName(classification);
        return new Result(Code.SUCCESS.getValue(), "修改成功");
    }

    //   查询所有仓库
    @PostMapping("/StashSelectAll")
    public Result StashSelectAll() {
        List<Stash> stashes = admin.StashSelectAll();
        return new Result(Code.SUCCESS.getValue(), "查询成功", stashes);
    }

    //   添加仓库
    @PostMapping("/StashAdd")
    public Result StashAdd(@RequestBody Stash stash) {
        List<Stash> stashes = admin.StashSelectAll();

        if (stashes.size() > 0) {
            List<Stash> collect = stashes.stream().filter(el -> {
                return el.getStashName().equals(stash.getStashName());
            }).collect(Collectors.toList());
            if (collect.size() > 0) {
                return new Result(Code.ERROR.getValue(), "存在相同的仓库名");
            }
        }

        admin.StashAdd(stash);
        return new Result(Code.SUCCESS.getValue(), "添加成功", stashes);
    }

    //   根据id修改仓库信息
    @PostMapping("/stashUpdateInformation")
    public Result stashUpdateInformation(@RequestBody Stash stash) {
        Stash stashes = admin.StashSelectAId(stash);
        if (stashes.getStashCapacity() >= stash.getStashTotalCapacity()) {
            return new Result(Code.ERROR.getValue(), "数据出错，仓库总容量不能低于仓库使用容量");
        }
        admin.stashUpdateInformation(stash);
        return new Result(Code.SUCCESS.getValue(), "修改成功");
    }


    //   分页查询所有管理员，不包含超级管理员
    @PostMapping("/AdminSelectLimit")
    public Result AdminSelectLimit(@RequestBody Page page) {

        if (page.getPageIndex()-1 < 0) {
            return new Result(Code.ERROR.getValue(), "数据出错，当前页面参数值不能小于零");
        }

        page.setPageIndex((page.getPageIndex()-1) * page.getPageSize());
        List<admins> admins = admin.AdminSelectAll();
        List<admins> admins1 = admin.AdminSelectLimit(page);

        HashMap<Object, Object> map = new HashMap<>();
        map.put("Total", admins.size());
        map.put("Data", admins1);
        return new Result(Code.SUCCESS.getValue(), "查询成功", map);
    }

    //   员工离职
    @PostMapping("/AdminUpdate")
    public Result AdminUpdate(@RequestBody admins admins) {
        admins admins1 = admin.AdminSelect(admins);
        if (admins1 == null) {
            return new Result(Code.ERROR.getValue(), "id/手机号不存在");
        }
        admin.AdminUpdate(admins);
        return new Result(Code.SUCCESS.getValue(), "修改成功");
    }
    //   添加新员工
    @PostMapping("/AdminAdd")
    public Result AdminAdd(@RequestBody admins admins) {

        admins admins1 = admin.AdminSelectPhone(admins);
        if (admins1 != null) {
            return new Result(Code.ERROR.getValue(), "该手机号已存在，请勿重复添加");
        }
        admins.setAdminLeave(1);
        admins.setAdminAuthority(2);
        admins.setAdminPassword("admin123");
        admin.AdminAdd(admins);
        return new Result(Code.SUCCESS.getValue(), "添加成功");
    }

    //   根据id/手机号查询员工
    @PostMapping("/AdminSelect")
    public Result AdminSelect(@RequestBody admins admins) {
        admins admins1 = admin.AdminSelectP(admins);
        if (admins1 == null) {
            return new Result(Code.ERROR.getValue(), "查询员工不存在");
        }
        return new Result(Code.SUCCESS.getValue(), "查询成功",admins1);
    }

    // 根据分类id删除分类
    @PostMapping("/DeleteClass")
    public Result DeleteClass(@RequestBody Classification classification) {
        // 查询该分类是否存在商品
        List<ProductClass> productClasses = admin.SelectClassificationProductAll(classification.getClassificationId());
        if (productClasses.size() > 0) {
            return new Result(Code.ERROR.getValue(), "有商品归属该分类因此无法删除");
        }
        admin.DeleteClass(classification.getClassificationId());
        return new Result(Code.SUCCESS.getValue(), "删除成功");
    }





}
